﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.GFT
{
    [Description("GFT:发邮件测试"), HotUpdate]
    public class MailVirtualAccountEditPlugIn : AbstractBillPlugIn
    {
        public override void ButtonClick(ButtonClickEventArgs e)
        {
            base.ButtonClick(e);
            if (e.Key.EqualsIgnoreCase("FTESTSEND"))
            {
                string str = Convert.ToString(this.Model.DataObject["FEmailAddress"]);
                if (!RegexUtil.IsEmail(str))
                {
                    this.View.ShowErrMessage("电子邮件地址不合法");
                }
                else
                {
                    List<string> list = new List<string>() { str };
                    DynamicObject obj2 = this.View.Model.GetValue("FMailServer") as DynamicObject;
                    if (obj2 == null)
                    {
                        this.View.ShowErrMessage("请设置邮件服务器");
                    }
                    else
                    {
                        string str2 = Convert.ToString(obj2["FMailServer"]);
                        int num = Convert.ToInt32(obj2["FSMTPPort"]);
                        string str3 = Convert.ToString(this.View.Model.GetValue("FUserName"));
                        string str4 = Convert.ToString(this.View.Model.GetValue("FPassword"));
                        bool flag = Convert.ToBoolean(obj2["FSSL"]);
                        string str5 = "邮件帐户设置测试邮件";
                        string str6 = "测试邮件发送成功！";
                        try
                        {
                            MailUtils.Sendmail(str, null, list, str5, str6, str2, num, flag, null, str3, str4);
                        }
                        catch (SmtpFailedRecipientsException exception)
                        {
                            this.View.ShowErrMessage($"该消息可能不会被发送到一个或多个收件人。错误消息：{exception.Message}");
                        }
                        catch (SmtpFailedRecipientException exception2)
                        {
                            this.View.ShowErrMessage($"发送到收件人错误。错误消息：{exception2.Message}");
                        }
                        catch (SmtpException exception3)
                        {
                            this.View.ShowErrMessage($"发送服务器或端口号设置不正确。。错误消息：{exception3.Message}");
                        }
                        catch (Exception exception4)
                        {
                            throw exception4;
                        }
                    }
                }
            }
        }
    }
}
